package foundation.SurModel.rndForest.sample;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import foundation.SurModel.rndForest.ClsQualIndice;
import foundation.SurModel.rndForest.RegQualIndice;
import foundation.dbUtil.DBException;
import foundation.dbUtil.DBUtil;
import foundation.fileUtil.DataCell;
import foundation.fileUtil.FileNameUtil;
import foundation.fileUtil.PropFileUtil;
import weka.core.Instances;

public class MLearnTbl {
	private static MLearnTbl instance = null;// 单例
	ArrayList<Integer> strtIdxs = new ArrayList<Integer>();
	private String algCfgFileName =  null;//算法配置文件名
	// 性能学习表
	private ArrayList<PerfLearnTblItem> perfSamTbl = new ArrayList<PerfLearnTblItem>();
	// 可靠性学习表
	private ArrayList<RelbLearnTblItem> relbSamTbl = new ArrayList<RelbLearnTblItem>();

	private MLearnTbl() {

	}

	public static MLearnTbl getInstance() {
		if (instance == null) {
			instance = new MLearnTbl();
		}
		return instance;
	}

	private ArrayList<Integer> selPerfTrainSamples() {
		ArrayList<Integer> idxs = new ArrayList<Integer>();
		int totSize = perfSamTbl.size();
//		String path = FileNameUtil.getPrjPath();
//		int samplesize = Integer
//				.parseInt(PropFileUtil.getInstance(path + "demo\\jssbrs\\cfgFiles\\NSRulSeqGAConf.properties")
//						.getParameterValue("sampleNum"));
		int samplesize = Integer
				.parseInt(PropFileUtil.getInstance(algCfgFileName)
						.getParameterValue("sampleNum"));
		// 从最近一次取3倍于sampleSize大小的样本，不足3倍sampleSize大小取最大的
		// 并且这些样本要包含真实值
		// int realSize=Math.min(totSize, 3*samplesize);
		int expectedSize =  totSize;
		PerfLearnTblItem item = null;
		int gotNum = 0;// 已采的样本数
		for (int i = totSize - 1; i >= 0; i--) {
			if (gotNum < expectedSize) {
				item = perfSamTbl.get(i);
				float resTime = item.getActualresTime();
				boolean canBeUsed = (Float.compare(resTime, 0f) > 0);
				if (canBeUsed) {
					idxs.add(i);
					gotNum++;
				}
			}
			else{//如果采集够了退出
				break;
			}
		}
		return idxs;
	}

	/*
	 * 得到分类模型质量的混淆矩阵
	 */
	private ArrayList<ArrayList<Integer>> getConfsMatrix(String type, int frmNo, int toNo,
			ArrayList<PerfLearnTblItem> perfL) {
		ArrayList<ArrayList<Integer>> confsMatrix = new ArrayList<ArrayList<Integer>>();
		for (int i = 0; i < 4; i++) {
			ArrayList<Integer> conf = new ArrayList<>();
			for (int j = 0; j < 4; j++) {
				conf.add(0);
			}
			confsMatrix.add(conf);
		}
		for (int i = frmNo; i <= toNo; i++) {
			if (type.equals("Max")) {
				int tempnum = confsMatrix.get(maxNoact - 1).get(maxNopre - 1);
				confsMatrix.get(maxNoact - 1).set(maxNopre - 1, tempnum + 1);
			}
			if (type.equals("Min")) {
				int tempnum = confsMatrix.get(minNoact - 1).get(minNopre - 1);
				confsMatrix.get(minNoact - 1).set(minNopre - 1, tempnum + 1);
			}
		}

		return confsMatrix;
	}

	private String changecfsMatToString(ArrayList<ArrayList<Integer>> cfsmatrix) {
		String changecfsMatToString = "";
		for (int i = 0; i < cfsmatrix.size(); i++) {
			for (int j = 0; j < cfsmatrix.get(0).size(); j++) {
				changecfsMatToString += " " + cfsmatrix.get(i).get(j);
			}
			System.out.println();
		}
		return changecfsMatToString;
	}

	private ArrayList<Integer> selRelbTrainSamples() {
		ArrayList<Integer> idxs = new ArrayList<Integer>();
		int totSize = relbSamTbl.size();
		for (int j = totSize - 1; j > totSize - 400; j++) {
			idxs.add(j);
		}
		return idxs;

	}

	private ArrayList<Float> computeAccuracyAndRP(int frmNo, int toNo) {
		ArrayList<Float> computeAccuracyAndRP = new ArrayList<>();

		float computeAccuracy = -1f;
		List<Float> realValue = new ArrayList<>();
		List<Float> predValue = new ArrayList<>();
		ArrayList<PerfLearnTblItem> perfL = getPerfSamTbl();
		for (int i = frmNo; i <= toNo; i++) {
			realValue.add(perfL.get(i).getActualresTime());
			predValue.add(perfL.get(i).getPredictResTime());
		}

		float mse = 0;
		float variance = 0;
		float mean = 0;
		for (int i = 0; i < realValue.size(); i++) {
			mse += (realValue.get(i) - predValue.get(i)) * (realValue.get(i) - predValue.get(i));
			mean += realValue.get(i);
		}
		mean = mean / realValue.size();
		for (int i = 0; i < realValue.size(); i++) {
			variance += (realValue.get(i) - mean) * (realValue.get(i) - mean);
		}
		computeAccuracy = 1 - mse / variance;
		computeAccuracyAndRP.add(computeAccuracy);

		float computeRP = -1f;

		int num1 = 0;
		int num2 = 0;
		for (int i = 0; i < realValue.size(); i++) {
			for (int j = i + 1; j < realValue.size(); j++) {
				if ((predValue.get(i) < predValue.get(j) && realValue.get(i) < realValue.get(j))
						|| (predValue.get(i) > predValue.get(j) && realValue.get(i) > realValue.get(j))
						|| (predValue.get(i) == predValue.get(j) && realValue.get(i) == realValue.get(j))) {
					num1++;
				}
				num2++;
			}
		}

		computeRP = 1.0f * num1 / num2;
		computeAccuracyAndRP.add(computeRP);

		return computeAccuracyAndRP;
	}

	/**
	 * 获取训练集以及训练集在weka中的内部表示
	 */
	// 未测试
	private List<PerfLearnTblItem> find(List<StringBuffer> MLDfcodes) {
		List<PerfLearnTblItem> findPerf = new ArrayList<>();
		PerfLearnTblItem perfItem = null;
		for (int i = 0; i < getPerfSamTbl().size(); i++) {
			perfItem = getPerfSamTbl().get(i);
			if (perfItem.getMlDFcodes().equals(MLDfcodes))
				findPerf.add(perfItem);
		}

		return findPerf;

	}

	// 未测试
	public float findRest(List<StringBuffer> MLDfcodes) {
		float findRest = -1f;
		List<PerfLearnTblItem> findRestItem = find(MLDfcodes);
		for (int i = findRestItem.size() - 1; i > 0; i--) {
			if (findRestItem.get(i).getActualresTime() != -1f) {
				findRest = findRestItem.get(i).getActualresTime();
				break;
			}

		}
		return findRest;
	}

	public PerfSamResult getPefSamResult(String mdlNm) {
		PerfSamResult samRlt = new PerfSamResult();
		// 获取选中的训练集下标
		ArrayList<Integer> idxs = selPerfTrainSamples();
		Instances instances = null;
		PerfLearnTblItem.setStrtIdxs(getStrtIdxs());
		// 获取列名字列表
		List<String> columnTypeNames = PerfLearnTblItem.getAttrs();
		// 采集的样本集
		ArrayList<PerfLearnTblItem> samples = new ArrayList<PerfLearnTblItem>();
		// 转成double类型数据集
		ArrayList<ArrayList<Double>> dataset = new ArrayList<ArrayList<Double>>();
		for (int i = 0; i < idxs.size(); i++) {
			PerfLearnTblItem item = perfSamTbl.get(idxs.get(idxs.size() - 1 - i));
			samples.add(item);
			dataset.add(item.toDblList(mdlNm));
		}
		try {
			instances = ConverterData.converterData(columnTypeNames, dataset);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		samRlt.setSamples(samples);
		samRlt.setInstances(instances);
		return samRlt;

	}

	public RelbSamResult getReblSamResult() {
		RelbSamResult samRlt = new RelbSamResult();
		// 获取选中的样本集下标
		ArrayList<Integer> idxs = selRelbTrainSamples();
		Instances instances = null;
		// 获取列名字列表
		List<String> columnTypeNames = PerfLearnTblItem.getAttrs();
		// 采集的样本集
		ArrayList<RelbLearnTblItem> samples = new ArrayList<RelbLearnTblItem>();
		// 转成double类型数据集
		ArrayList<ArrayList<Double>> dataset = new ArrayList<ArrayList<Double>>();
		for (int idx : idxs) {
			RelbLearnTblItem item = relbSamTbl.get(idx);
			samples.add(item);
			dataset.add(item.toDblList());
		}
		try {
			instances = ConverterData.converterData(columnTypeNames, dataset);
		} catch (Exception e) {
			e.printStackTrace();
		}
		samRlt.setSamples(samples);
		samRlt.setInstances(instances);
		return samRlt;
	}

	/**
	 * 获取最大使用率处理器分类模型的质量指标
	 * 
	 * @return
	 */

	public List<ClsQualIndice> getProMaxUtilClsQulIdx() {
		List<ClsQualIndice> proMaxUtilClsQulIdx = new ArrayList<>();
		ArrayList<PerfLearnTblItem> perfL = getPerfSamTbl();
		PropFileUtil propFileUtil = PropFileUtil
				.getInstance(FileNameUtil.getPrjPath() + "demo\\jssbrs\\cfgFiles\\NSRulSeqGAConf.properties");
		int actualNum = Integer.parseInt(propFileUtil.getParameterValue("sampleNum"));
		int predicNum = Integer.parseInt(propFileUtil.getParameterValue("predictNum"));
		int completeTraAndPreNum = perfL.size() / (actualNum + predicNum);
		for (int i = 0; i < completeTraAndPreNum; i++) {
			ClsQualIndice clsQualIndice = new ClsQualIndice();
			clsQualIndice.setFrmNo(i * (actualNum + predicNum));
			clsQualIndice.setToNo(i * (actualNum + predicNum) + actualNum - 1);
			clsQualIndice.setClsProbNm("Max");
			clsQualIndice.setDataSetType("train");
			clsQualIndice.setConfsMatrix(getConfsMatrix("Max", i * (actualNum + predicNum),
					i * (actualNum + predicNum) + actualNum - 1, perfL));
			proMaxUtilClsQulIdx.add(clsQualIndice);

			ClsQualIndice clsQualIndice1 = new ClsQualIndice();
			clsQualIndice1.setFrmNo(i * (actualNum + predicNum) + actualNum);
			clsQualIndice1.setToNo((i + 1) * (actualNum + predicNum) - 1);
			clsQualIndice1.setClsProbNm("Max");
			clsQualIndice1.setDataSetType("predict");
			clsQualIndice1.setConfsMatrix(getConfsMatrix("Max", i * (actualNum + predicNum) + actualNum,
					(i + 1) * (actualNum + predicNum) - 1, perfL));
			proMaxUtilClsQulIdx.add(clsQualIndice1);

		}
		int remainder = perfL.size() - completeTraAndPreNum * (actualNum + predicNum);
		if (remainder == 0) {
			return proMaxUtilClsQulIdx;
		} else if (remainder <= actualNum) {
			ClsQualIndice clsQualIndice = new ClsQualIndice();
			clsQualIndice.setFrmNo(completeTraAndPreNum * (actualNum + predicNum));
			clsQualIndice.setToNo(perfL.size() - 1);
			clsQualIndice.setClsProbNm("Max");
			clsQualIndice.setDataSetType("train");
			clsQualIndice.setConfsMatrix(
					getConfsMatrix("Max", (completeTraAndPreNum * (actualNum + predicNum)), perfL.size() - 1, perfL));
			proMaxUtilClsQulIdx.add(clsQualIndice);
		} else {
			ClsQualIndice clsQualIndice = new ClsQualIndice();
			clsQualIndice.setFrmNo(completeTraAndPreNum * (actualNum + predicNum));
			clsQualIndice.setToNo(completeTraAndPreNum * (actualNum + predicNum) + actualNum - 1);
			clsQualIndice.setClsProbNm("Max");
			clsQualIndice.setDataSetType("train");
			clsQualIndice.setConfsMatrix(getConfsMatrix("Max", (completeTraAndPreNum * (actualNum + predicNum)),
					completeTraAndPreNum * (actualNum + predicNum) + actualNum - 1, perfL));
			proMaxUtilClsQulIdx.add(clsQualIndice);

			ClsQualIndice clsQualIndice1 = new ClsQualIndice();
			clsQualIndice1.setFrmNo(completeTraAndPreNum * (actualNum + predicNum) + actualNum);
			clsQualIndice1.setToNo(perfL.size() - 1);
			clsQualIndice1.setClsProbNm("Max");
			clsQualIndice1.setDataSetType("predict");
			clsQualIndice1.setConfsMatrix(getConfsMatrix("Max",
					(completeTraAndPreNum * (actualNum + predicNum) + actualNum), perfL.size() - 1, perfL));
			proMaxUtilClsQulIdx.add(clsQualIndice1);
		}

		return proMaxUtilClsQulIdx;
	}

	/**
	 * 获取最小使用率处理器分类模型的质量指标
	 * 
	 * @return
	 */
	public List<ClsQualIndice> getProMinUtilClsQulIdx() {
		List<ClsQualIndice> proMinUtilClsQulIdx = new ArrayList<>();
		ArrayList<PerfLearnTblItem> perfL = getPerfSamTbl();
		PropFileUtil propFileUtil = PropFileUtil
				.getInstance(FileNameUtil.getPrjPath() + "demo\\jssbrs\\cfgFiles\\NSRulSeqGAConf.properties");
		int actualNum = Integer.parseInt(propFileUtil.getParameterValue("sampleNum"));
		int predicNum = Integer.parseInt(propFileUtil.getParameterValue("predictNum"));

		int completeTraAndPreNum = perfL.size() / (actualNum + predicNum);
		for (int i = 0; i < completeTraAndPreNum; i++) {

			ClsQualIndice clsQualIndice = new ClsQualIndice();
			clsQualIndice.setFrmNo(i * (actualNum + predicNum));
			clsQualIndice.setToNo(i * (actualNum + predicNum) + actualNum - 1);
			clsQualIndice.setClsProbNm("Min");
			clsQualIndice.setClsProbNm("train");
			clsQualIndice.setConfsMatrix(getConfsMatrix("Min", i * (actualNum + predicNum),
					i * (actualNum + predicNum) + actualNum - 1, perfL));
			proMinUtilClsQulIdx.add(clsQualIndice);
			ClsQualIndice clsQualIndice1 = new ClsQualIndice();
			clsQualIndice1.setFrmNo(i * (actualNum + predicNum) + actualNum);
			clsQualIndice1.setToNo((i + 1) * (actualNum + predicNum) - 1);
			clsQualIndice1.setClsProbNm("Min");
			clsQualIndice1.setDataSetType("predict");
			clsQualIndice1.setConfsMatrix(getConfsMatrix("Min", i * (actualNum + predicNum) + actualNum,
					(i + 1) * (actualNum + predicNum) - 1, perfL));
			proMinUtilClsQulIdx.add(clsQualIndice1);

		}
		int remainder = perfL.size() - completeTraAndPreNum * (actualNum + predicNum);
		if (remainder == 0) {
			return proMinUtilClsQulIdx;
		} else if (remainder <= actualNum) {
			ClsQualIndice clsQualIndice = new ClsQualIndice();
			clsQualIndice.setFrmNo(completeTraAndPreNum * (actualNum + predicNum));
			clsQualIndice.setToNo(perfL.size() - 1);
			clsQualIndice.setClsProbNm("Min");
			clsQualIndice.setDataSetType("train");
			clsQualIndice.setConfsMatrix(
					getConfsMatrix("Min", (completeTraAndPreNum * (actualNum + predicNum)), perfL.size() - 1, perfL));
			proMinUtilClsQulIdx.add(clsQualIndice);
		} else {
			ClsQualIndice clsQualIndice = new ClsQualIndice();
			clsQualIndice.setFrmNo(completeTraAndPreNum * (actualNum + predicNum));
			clsQualIndice.setToNo(completeTraAndPreNum * (actualNum + predicNum) + actualNum - 1);
			clsQualIndice.setClsProbNm("Min");
			clsQualIndice.setDataSetType("train");
			clsQualIndice.setConfsMatrix(getConfsMatrix("Min", (completeTraAndPreNum * (actualNum + predicNum)),
					completeTraAndPreNum * (actualNum + predicNum) + actualNum - 1, perfL));
			proMinUtilClsQulIdx.add(clsQualIndice);

			ClsQualIndice clsQualIndice1 = new ClsQualIndice();
			clsQualIndice1.setFrmNo(completeTraAndPreNum * (actualNum + predicNum) + actualNum);
			clsQualIndice1.setToNo(perfL.size() - 1);
			clsQualIndice1.setClsProbNm("Min");
			clsQualIndice1.setDataSetType("predict");
			clsQualIndice1.setConfsMatrix(getConfsMatrix("Min",
					(completeTraAndPreNum * (actualNum + predicNum) + actualNum), perfL.size() - 1, perfL));
			proMinUtilClsQulIdx.add(clsQualIndice1);
		}
		return proMinUtilClsQulIdx;
	}

	/**
	 * 获取响应时间回归模型的质量指标
	 * 
	 * @return
	 */
	public List<RegQualIndice> getRstRegQulIdx() {
		List<RegQualIndice> rstRegQulIdx = new ArrayList<>();
		ArrayList<PerfLearnTblItem> perfL = getPerfSamTbl();
		PropFileUtil propFileUtil = PropFileUtil
				.getInstance(FileNameUtil.getPrjPath() + "demo\\jssbrs\\cfgFiles\\NSGAConf.properties");
		int actualNum = Integer.parseInt(propFileUtil.getParameterValue("sampleNum"));
		int predicNum = Integer.parseInt(propFileUtil.getParameterValue("predictNum"));
		int completeTraAndPreNum = perfL.size() / (actualNum + predicNum);
		for (int i = 0; i < completeTraAndPreNum; i++) {
			RegQualIndice regQualIndice = new RegQualIndice();
			regQualIndice.setFrmNo(i * (actualNum + predicNum));
			regQualIndice.setToNo(i * (actualNum + predicNum) + actualNum - 1);
			regQualIndice.setDataSetType("train");
			ArrayList<Float> computeAccuracyAndRP = computeAccuracyAndRP(i * (actualNum + predicNum),
					i * (actualNum + predicNum) + actualNum - 1);
			regQualIndice.setAccuracy(computeAccuracyAndRP.get(0));
			regQualIndice.setRp(computeAccuracyAndRP.get(1));
			rstRegQulIdx.add(regQualIndice);

			RegQualIndice regQualIndice1 = new RegQualIndice();
			regQualIndice1.setFrmNo(i * (actualNum + predicNum) + actualNum);
			regQualIndice1.setToNo((i + 1) * (actualNum + predicNum) - 1);
			regQualIndice1.setDataSetType("predict");
			ArrayList<Float> computeAccuracyAndRP1 = computeAccuracyAndRP(i * (actualNum + predicNum) + actualNum,
					(i + 1) * (actualNum + predicNum) - 1);
			regQualIndice1.setAccuracy(computeAccuracyAndRP1.get(0));
			regQualIndice1.setRp(computeAccuracyAndRP1.get(1));
			rstRegQulIdx.add(regQualIndice1);
		}
		int remainder = perfL.size() - completeTraAndPreNum * (actualNum + predicNum);
		if (remainder == 0) {
			return rstRegQulIdx;
		} else if (remainder <= actualNum) {
			RegQualIndice regQualIndice = new RegQualIndice();
			regQualIndice.setFrmNo(completeTraAndPreNum * (actualNum + predicNum));
			regQualIndice.setToNo(perfL.size() - 1);
			regQualIndice.setDataSetType("train");
			ArrayList<Float> computeAccuracyAndRP = computeAccuracyAndRP(completeTraAndPreNum * (actualNum + predicNum),
					perfL.size() - 1);

			regQualIndice.setAccuracy(computeAccuracyAndRP.get(0));
			regQualIndice.setRp(computeAccuracyAndRP.get(1));
			rstRegQulIdx.add(regQualIndice);
		} else {
			RegQualIndice regQualIndice = new RegQualIndice();
			regQualIndice.setFrmNo(completeTraAndPreNum * (actualNum + predicNum));
			regQualIndice.setToNo(completeTraAndPreNum * (actualNum + predicNum) + actualNum - 1);
			regQualIndice.setDataSetType("train");
			ArrayList<Float> computeAccuracyAndRP = computeAccuracyAndRP(completeTraAndPreNum * (actualNum + predicNum),
					completeTraAndPreNum * (actualNum + predicNum) + actualNum - 1);

			regQualIndice.setAccuracy(computeAccuracyAndRP.get(0));
			regQualIndice.setRp(computeAccuracyAndRP.get(1));
			rstRegQulIdx.add(regQualIndice);

			RegQualIndice regQualIndice1 = new RegQualIndice();
			regQualIndice1.setFrmNo(completeTraAndPreNum * (actualNum + predicNum) + actualNum);
			regQualIndice1.setToNo(perfL.size() - 1);
			regQualIndice1.setDataSetType("predict");

			ArrayList<Float> computeAccuracyAndRP1 = computeAccuracyAndRP(
					completeTraAndPreNum * (actualNum + predicNum) + actualNum, perfL.size() - 1);

			regQualIndice1.setAccuracy(computeAccuracyAndRP1.get(0));
			regQualIndice1.setRp(computeAccuracyAndRP1.get(1));
			rstRegQulIdx.add(regQualIndice1);

		}
		return rstRegQulIdx;
	}

	/**
	 * 获取可靠性回归模型的质量指标
	 * 
	 * @return
	 */
	public List<RegQualIndice> getRelbRegQulIdx() {
		return null;
	}

	/**
	 * 输出性能学习表
	 * 
	 * @param runID：算法运行的id
	 * @param startTime:开始时间
	 * @param endTime：结束时间
	 * @param host：主机号
	 * @param caseName：案例名
	 * @param algNm：算法名
	 * @return
	 */
	public List<List<DataCell>> outPerfTbl(String runID, String startTime, String endTime, String host, String caseName,
			String algNm) {
		List<List<DataCell>> outPerfTbl = new ArrayList<List<DataCell>>();
		List<DataCell> oneRow = new ArrayList<>();
		DataCell dataCell = new DataCell();
		dataCell.setName(runID);
		oneRow.add(dataCell);
		DataCell dataCell1 = new DataCell();
		dataCell1.setName(startTime);
		oneRow.add(dataCell1);
		DataCell dataCell2 = new DataCell();
		dataCell2.setName(endTime);
		oneRow.add(dataCell2);
		DataCell dataCell3 = new DataCell();
		dataCell3.setName(host);
		oneRow.add(dataCell3);
		DataCell dataCell4 = new DataCell();
		dataCell4.setName(caseName);
		oneRow.add(dataCell4);
		DataCell dataCell5 = new DataCell();
		dataCell5.setName(algNm);
		oneRow.add(dataCell5);

		outPerfTbl.add(oneRow);

		return outPerfTbl;
	}

	/**
	 * 输出可靠性学习表
	 * 
	 * @param runID：算法运行的id
	 * @param startTime:开始时间
	 * @param endTime：结束时间
	 * @param host：主机号
	 * @param caseName：案例名
	 * @param algNm：算法名
	 * @return
	 */
	public List<List<DataCell>> outRelbTbl(String runID, String startTime, String endTime, String host, String caseName,
			String algNm) {
		List<List<DataCell>> outRelbTbl = new ArrayList<List<DataCell>>();
		List<DataCell> oneRow = new ArrayList<>();
		DataCell dataCell = new DataCell();
		dataCell.setName(runID);
		oneRow.add(dataCell);
		DataCell dataCell1 = new DataCell();
		dataCell1.setName(startTime);
		oneRow.add(dataCell1);
		DataCell dataCell2 = new DataCell();
		dataCell2.setName(endTime);
		oneRow.add(dataCell2);
		DataCell dataCell3 = new DataCell();
		dataCell3.setName(host);
		oneRow.add(dataCell3);
		DataCell dataCell4 = new DataCell();
		dataCell4.setName(caseName);
		oneRow.add(dataCell4);
		DataCell dataCell5 = new DataCell();
		dataCell5.setName(algNm);
		oneRow.add(dataCell5);

		outRelbTbl.add(oneRow);

		return outRelbTbl;
	}

	/**
	 * 输出性能代理模型质量指标
	 * 
	 * @param runID
	 * @param startTime
	 * @param endTime
	 * @param host
	 * @param caseName
	 * @param algNm
	 * @return
	 */
	public List<List<DataCell>> outPerfSMQul(String runID, String host, String caseName, String algNm) {
		List<List<DataCell>> outPerfSMQul = new ArrayList<List<DataCell>>();

		List<RegQualIndice> rstRegQulIdx = getRstRegQulIdx();

		/*
		 * 在表格开头，输出表的属性
		 */
		List<DataCell> runIDprop = new ArrayList<>();
		DataCell runIdNM = new DataCell();
		runIdNM.setName("RunID");
		runIDprop.add(runIdNM);
		DataCell runIdprop = new DataCell();
		runIdprop.setName("" + runID);
		runIDprop.add(runIdprop);
		outPerfSMQul.add(runIDprop);

		List<DataCell> hostprop = new ArrayList<>();
		DataCell hostNM = new DataCell();
		hostNM.setName("host");
		hostprop.add(hostNM);
		DataCell hostpro = new DataCell();
		hostpro.setName("" + host);
		hostprop.add(hostpro);
		outPerfSMQul.add(hostprop);

		List<DataCell> caseNameprop = new ArrayList<>();
		DataCell caseNameNM = new DataCell();
		caseNameNM.setName("caseName");
		caseNameprop.add(caseNameNM);
		DataCell caseNamepro = new DataCell();
		caseNamepro.setName("" + caseName);
		caseNameprop.add(caseNamepro);
		outPerfSMQul.add(caseNameprop);

		List<DataCell> algNprop = new ArrayList<>();
		DataCell algNNM = new DataCell();
		algNNM.setName("algN");
		algNprop.add(algNNM);
		DataCell algNpro = new DataCell();
		algNpro.setName("" + algNm);
		algNprop.add(algNpro);
		outPerfSMQul.add(algNprop);
		/*
		 * 表格的标题，包括 训练次数，最大使用率处理器的分类模型度量 最小使用率处理器的分类模型度量，响应时间预测模型度量之Accuracy
		 * 响应时间预测模型度量之RP
		 */
		List<DataCell> title = new ArrayList<>();
		DataCell trianTimes = new DataCell();
		trianTimes.setName("训练次数");
		title.add(trianTimes);
		DataCell restDataCellNM = new DataCell();
		restDataCellNM.setName("响应时间预测模型度量之Accuracy");
		title.add(restDataCellNM);
		DataCell restDataCellNM1 = new DataCell();
		restDataCellNM1.setName("响应时间预测模型度量之RP");
		title.add(restDataCellNM1);
		outPerfSMQul.add(title);

		for (int i = 0; i < rstRegQulIdx.size(); i++) {
			List<DataCell> oneRow = new ArrayList<>();
			DataCell trainTime = new DataCell();
			trainTime.setName("" + (i + 1));
			oneRow.add(trainTime);
			DataCell restDataCell = new DataCell();
			restDataCell.setName("" + rstRegQulIdx.get(i).getAccuracy());
			oneRow.add(restDataCell);
			DataCell restDataCell1 = new DataCell();
			restDataCell1.setName("" + rstRegQulIdx.get(i).getRp());
			oneRow.add(restDataCell1);
			outPerfSMQul.add(oneRow);
		}

		// String startTime, String endTime,
		return outPerfSMQul;
	}

	/**
	 * 输出可靠性代理模型质量指标
	 * 
	 * @param runID
	 * @param startTime
	 * @param endTime
	 * @param host
	 * @param caseName
	 * @param algN
	 * @return
	 */
	public List<List<DataCell>> outRelbSMQul(String runID, String host, String caseName, String algN) {
		// String startTime, String endTime,
		// getRelbRegQulIdx
		return null;
	}

	public boolean outPerfTblToDB(String runID) {
		String dbCfgFileName = "src/foundation/dbUtil/dbConf.properties";
		DBUtil dbUtil = DBUtil.getInstance(dbCfgFileName);
		boolean rlt = true;
		try {
			// 向主表T_PerfLearn中增加记录
			String sqlTxt = "insert into t_PerfLearnn(runID,mlDFcodes,actualResTime,predictResTime)";
			sqlTxt += "values(?,?,?,?)";// 共8个参数
			// 产生一个预编译的命令对象
			PreparedStatement pStmt = dbUtil.getPrepStmt(sqlTxt);

			ArrayList<PerfLearnTblItem> perf = getPerfSamTbl();
			int size = perf.size();
			for (int i = 0; i < size; i++) {
				String mlDFcodes = perf.get(i).getMlDFcodes().toString();
				float actualResTime = perf.get(i).getActualresTime();
				float predictResTime = perf.get(i).getPredictResTime();
				pStmt.setString(1, runID);
				pStmt.setString(2, mlDFcodes);
				pStmt.setFloat(3, actualResTime);
				pStmt.setFloat(4, predictResTime);

				pStmt.executeUpdate();
			}
			dbUtil.closeConn();
		} catch (SQLException e) {

			rlt = false;
			e.printStackTrace();

		} catch (DBException e) {
			rlt = false;
			e.printStackTrace();
		}
		return rlt;
	}

	public boolean outRelbTblToDB() {
		String dbCfgFileName = "src/foundation/dbUtil/dbConf.properties";
		DBUtil dbUtil = DBUtil.getInstance(dbCfgFileName);
		boolean rlt = true;
		try {
			// 向主表T_PerfLearn中增加记录
			String sqlTxt = "insert into t_RelbLearn(mlDFcodes,actualRelbVal,predictRelbVal)";
			sqlTxt += "values(?,?,?)";// 共8个参数
			// 产生一个预编译的命令对象
			PreparedStatement pStmt = dbUtil.getPrepStmt(sqlTxt);

			ArrayList<RelbLearnTblItem> relb = getRelbSamTbl();
			int size = relb.size();
			for (int i = 0; i < size; i++) {
				String mlDFcodes = relb.get(i).getMlDFcodes().toString();
				float actualRelbVal = relb.get(i).getActualRelbVal();
				float predictRelbVal = relb.get(i).getPredictRelbVal();
				pStmt.setString(1, mlDFcodes);
				pStmt.setFloat(2, actualRelbVal);
				pStmt.setFloat(3, predictRelbVal);

				pStmt.executeUpdate();
			}
			dbUtil.closeConn();

		} catch (SQLException e) {
			rlt = false;
			e.printStackTrace();

		} catch (DBException e) {
			rlt = false;
			e.printStackTrace();
		}

		return rlt;
	}

	/*
	 * public List<StringBuffer> getInitFdCodes() { List<StringBuffer> fdCodes =
	 * new ArrayList<StringBuffer>(); for (int i = 0; i < 4; i++) { float
	 * procSpd = 10.0f; StringBuffer spd = new
	 * StringBuffer(String.valueOf(procSpd)); fdCodes.add(spd); }
	 * fdCodes.add(new StringBuffer("server1")); fdCodes.add(new
	 * StringBuffer("server2")); fdCodes.add(new StringBuffer("server3"));
	 * fdCodes.add(new StringBuffer("server4")); fdCodes.add(new
	 * StringBuffer("server4")); fdCodes.add(new StringBuffer("server2"));
	 * fdCodes.add(new StringBuffer("server2")); fdCodes.add(new
	 * StringBuffer("server2")); fdCodes.add(new StringBuffer("server4"));
	 * fdCodes.add(new StringBuffer("WebServer"));
	 * 
	 * return fdCodes; }
	 */

	public static void main(String[] args) {

	}

	public void addRelbItem(RelbLearnTblItem item) {
		relbSamTbl.add(item);
	}

	public void addPerfItem(PerfLearnTblItem item) {
		perfSamTbl.add(item);
	}

	public ArrayList<PerfLearnTblItem> getPerfSamTbl() {
		return perfSamTbl;
	}

	public void setPerfSamTbl(ArrayList<PerfLearnTblItem> perfSamTbl) {
		this.perfSamTbl = perfSamTbl;
	}

	public ArrayList<RelbLearnTblItem> getRelbSamTbl() {
		return relbSamTbl;
	}

	public ArrayList<Integer> getStrtIdxs() {
		return strtIdxs;
	}

	public void setStrtIdxs(ArrayList<Integer> strtIdxs) {
		// PerfLearnTblItem.getStrtIdxs();
		this.strtIdxs = strtIdxs;
	}
	
	public void clearML(){
		ArrayList<PerfLearnTblItem> getPerfSamTbl = getPerfSamTbl();
		getPerfSamTbl.clear();
		ArrayList<RelbLearnTblItem> getRelbSamTbl = getRelbSamTbl();
		getRelbSamTbl.clear();
		
	}

	public void setRelbSamTbl(ArrayList<RelbLearnTblItem> relbSamTbl) {
		PerfLearnTblItem.setStrtIdxs(strtIdxs);
		RelbLearnTblItem.setStrtIdxs(strtIdxs);
	}

	public String getAlgCfgFileName() {
		return algCfgFileName;
	}

	public void setAlgCfgFileName(String algCfgFileName) {
		this.algCfgFileName = algCfgFileName;
	}

}
